<?php
namespace Com\Pay;

class MeilianPay{

    private $config = array();


    public function __construct($send_data){
        $this->config = $send_data;
    }

    /*生成支付连接*/
    public function getUrl($order_info,$other = array()){
        /* 获取提交的订单交易金额 */
        $price = $order_info['paymoney'];
        //支付成功后跳转地址
        $returnUrl    = $order_info['return_url'];
        if(!empty($other['callback'])){
            $notifyUrl = $other['callback'];
        }else{
            /* 获取提交的后台回调地址 */
            $notifyUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/Api/NotifyCallback/MeilianPayNoyify';
        }

        $order_sn = $order_info['ordernumber'];
        $post_data = array(
            'notify_url' =>  $notifyUrl,
            'data' => array(
                'partnerid' => $this->config['mch_id'],
//                'payid' => ($order_info['wxweb'] == 'WX_PUBLIC') ? '116' : '119',
                'payid' => 0,
                'coop_orderid' => $order_sn,
                'create_ip' => get_client_ip(),
                'amount' => $price,
                'userid' => '1',
                'product' => 'Cbpay',
                'wxweb' => $order_info['wxweb'],
                'driverid' => '123456',
                'extrdata'=> $this->config['mch_id']
            ),
        );
        $ContentSign = $this->setParam($post_data['data'],$this->config['mch_key'],false); // 计算出Sign
        $post_data['data']['sign'] = $ContentSign; // Sign签名
        $post_data['data'] = json_encode($post_data['data']);
        $tar_url = 'http://pay.cp.meiliangame.com/app/pay';
        $res = json_decode(https_request($tar_url,$post_data),true);
        if($res['status'] == 1 && $res['qrcode_url'] != ''){
            return $res['qrcode_url'];
        }else{
            return $res['pay_url'];
        }
        return '';
    }

    private function setParam($param,$key,$IS_Notify){
        $AddSignParams = array('code' , 'errorcode' , 'errormsg' , 'sporderid' , 'orderid' , 'amount' , 'paydate' , 'product' , 'userid');
        $AddSignParams1 = array('partnerid' , 'payid' , 'coop_orderid' , 'create_ip' , 'amount' , 'product' , 'driverid');
        $str = "";
        ksort($param);
        foreach($param as $key1=>$val)  {
            if($IS_Notify && !in_array($key1, $AddSignParams)) continue;
            if(!$IS_Notify && !in_array($key1, $AddSignParams1)) continue;
            $str.=$key1."=".$val."&";
        }
        $str = rtrim($str,'&');
//        var_dump($str);die;
        $sign = md5($str . '&key=' . $key);
        return $sign;
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $data = parseData();
        $MySign = $this->setParam($data,$this->config['mch_key'],true);
        if($data['sign'] == $MySign){
            if($data['code'] == '1'){
                return true;
            }else{
                // 失败
                return false;
            }
        }else{
            // 失败
            return false;
        }
    }


}
